package org.jfk.sys.dictionary;

import org.apache.log4j.Logger;
import org.jfk.core.model.SqlFilter;
import org.jfk.sys.user.User;

import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Table;
import com.jfinal.plugin.activerecord.TableMapping;
/**
mysql> show full columns from data_dict_type;
+---------+--------------+-----------------+------+-----+---------+----------------+---------------------------------+------------------+
| Field   | Type         | Collation       | Null | Key | Default | Extra          | Privileges                      | Comment          |
+---------+--------------+-----------------+------+-----+---------+----------------+---------------------------------+------------------+
| id      | int(11)      | NULL            | NO   | PRI | NULL    | auto_increment | select,insert,update,references |                  |
| enName  | varchar(255) | utf8_general_ci | NO   |     | NULL    |                | select,insert,update,references | 英文名称         |
| cnName  | varchar(255) | utf8_general_ci | NO   |     | NULL    |                | select,insert,update,references | 中文名称         |
| desc    | varchar(255) | utf8_general_ci | YES  |     | NULL    |                | select,insert,update,references | 数据字典类型描述 |
| creator | int(11)      | NULL            | NO   |     | NULL    |                | select,insert,update,references |                  |
+---------+--------------+-----------------+------+-----+---------+----------------+---------------------------------+------------------+ * 
 * @author jxf
 * 2014年12月13日
 */

@SuppressWarnings("serial")
public class DictType extends Model<DictType>{
	private static Logger logger = Logger.getLogger(DictType.class);	
	public static final DictType dao;
	public static final Table table;
	public static final String tableName;
	static {
		dao = new DictType();
		table = TableMapping.me().getTable(DictType.class);
		tableName = table.getName();
	}
	
	public DictType findById(Object id){
		return dao.findFirst("select a.*,b.loginName creator_loginName"
				+ " from "+tableName+" a left outer join "+User.tableName+" b on a.creator=b.id"
				+ " where a.id=?",id);
	}

	public Page<DictType> paginate(DictTypeController controller){
		String select = " select "+tableName+".*,ot1.loginName creator_loginName,ot2.dict_value status_value ";
		String from =" from "+tableName;
		String join = " left outer join "+User.tableName+" ot1 on ot1.id="+tableName+".creator";
		join = join + " left outer join "+Dict.tableName+" ot2 on ot2.id="+tableName+".status";
		String where = " where 1=1 ";
		
		SqlFilter filter = controller.genJsonFilter();
		
		//处理列表查询的智能搜索
		String search = controller.getPara("search");	

		if (search != null && !"".equals(search.trim())) {
			SqlFilter searchFilter = new SqlFilter();
			searchFilter.add("and", tableName+".id", "eq", search);
			searchFilter.add("or", tableName+".enName", "like", search);
			searchFilter.add("or", tableName+".cnName", "like", search);
			searchFilter.add("or", tableName+".description", "like", search);
			searchFilter.end();
			filter.add(searchFilter);
			controller.setAttr("search", search);
		}
		
		controller.addPageAttr();
		String sqlExceptSelect = from+join+where+filter.sqlText;
		sqlExceptSelect = sqlExceptSelect + controller.sqlSort;
		logger.debug(filter);
		Page<DictType> page = dao.paginate(controller.currentPage, controller.limit, select, sqlExceptSelect, filter.params.toArray());
		return page;		
	}	
	
}
